Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the IncorrectStaticContextHintError as a new hint #6703 #6899

Merged

Conversation

junichi11
Copy link
Member

@junichi11 junichi11 commented Jan 1, 2024

Note:
Check only $this in TypeDeclarations because Frameworks may use $this in the global scope.
e.g. CakePHP framework can use $this(e.g. $this->Html->link()) in the global scope of view files.
(see: https://book.cakephp.org/5/en/views.html)

Example:

class Example {
    private int $field = 1;

    public static function staticMethod(): void {
        $this->field; // error
    }

    public function method(): void {
        $staticClosure = static function() {
            var_dump($this); // error
        };
        $staticArrow = static fn() => $this; // error
        echo $this->field;
    }
}

nb-php-gh-6703-incorrect-this-in-static-context

- apache#6703
- Show errors when `$this` is used in static context(static methods, static closures, static arrow functions)
- Add unit tests

Note:
Check only $this in TypeDeclarations because Frameworks may use `$this` in the global scope.
e.g. CakePHP framework can use `$this`(e.g. `$this->Html->link()`) in the global scope of view files.
(see: https://book.cakephp.org/5/en/views.html)

Example:
```php
class Example {
    private int $field = 1;

    public static function staticMethod(): void {
        $this->field; // error
    }

    public function method(): void {
        $staticClosure = static function() {
            var_dump($this); // error
        };
        $staticArrow = static fn() => $this; // error
        echo $this->field;
    }
}
```
@junichi11 junichi11 added the PHP [ci] enable extra PHP tests (php/php.editor) label Jan 1, 2024
@junichi11 junichi11 added this to the NB21 milestone Jan 1, 2024
@junichi11 junichi11 linked an issue Jan 1, 2024 that may be closed by this pull request
@junichi11 junichi11 requested a review from tmysik January 1, 2024 11:35
Copy link
Member

@tmysik tmysik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thank you!

@junichi11
Copy link
Member Author

@tmysik Thank you for your quick review! Merging.

@junichi11 junichi11 merged commit 7c39099 into apache:master Jan 1, 2024
@junichi11 junichi11 deleted the php-gh-6703-this-in-static-context branch January 1, 2024 11:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PHP [ci] enable extra PHP tests (php/php.editor)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Give a hint using $this in static method.
2 participants